package com.hmyuan.controller.front;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hmyuan.domain.Article;
import com.hmyuan.domain.Tag;
import com.hmyuan.service.ArticleService;
import com.hmyuan.service.CommentService;
import com.hmyuan.service.TagService;
import com.hmyuan.util.images.ValidateCode;

/**
 * IndexController
 * @author wbtianhl
 *
 */
@Controller
@RequestMapping("")
public class IndexController {

	@Resource
	ArticleService articleService;
	@Resource
	CommentService commentService;
	@Resource
	TagService tagService;
	/**
	 *首页
	 */
	@RequestMapping(value="", method = RequestMethod.GET)
	public String index(HttpServletRequest request,Model model){
		
		request.setAttribute("navflag", "");
		return "/front/index";
	}
	/**
	 *检索
	 */
	@RequestMapping(value="search", method = RequestMethod.GET)
	public String search(HttpServletRequest request,String title,Model model){
		
		request.setAttribute("searchtitle", title);
		
		request.setAttribute("titleset", "检索："+title);
		//设置页面描述
		request.setAttribute("siteTitle", title+"-坏银的博客");
		return "/front/index";
	}
	/**
	 *单页内容显示（可在线编辑）
	 */
	@RequestMapping(value="page/{password}", method = RequestMethod.GET)
	public String about(HttpServletRequest request,Model model,@PathVariable String password){
		
		if(null==password&&"".equals(password)) return "/front/index";

		Article article = articleService.selectByPassword(password);

		if(null==article) return "/front/index";
		
		request.setAttribute("commentList", commentService.selectListByArticleid(article.getId()));
		
		/*浏览量加一*/
		Article articleup = new Article();
		articleup.setClicknum(article.getClicknum()+1);
		articleup.setId(article.getId());
		articleService.updateByPrimaryKeySelective(articleup);
		
		request.setAttribute("article", article);
		
		request.setAttribute("navflag", password);
		
		//设置页面描述
		request.setAttribute("siteTitle", article.getTitle()+"-坏银的博客");

		return "/front/page/page";
	}
	/**
	 *单页内容显示（需要代码编辑）
	 */
	@RequestMapping(value="o0o/{url}", method = RequestMethod.GET)
	public String o0o(HttpServletRequest request,Model model,@PathVariable String url){
		
		if(null==url&&"".equals(url)) return "/front/index";
		
		request.setAttribute("navflag","o0o"+url);
		return "/front/o0o/"+url;
	}
	
	/**
	 *查询标签列表带使用数
	 */
	@ResponseBody
	@RequestMapping(value="taglistWithCount")
	public List<Tag> taglistWithCount(HttpServletRequest request,Model model){
		
		return tagService.selectListWithCount();
	}
	/**
	 *根据标签检索文章
	 */
	@RequestMapping(value="tag/{id}/{name}", method = RequestMethod.GET)
	public String searchbytag(HttpServletRequest request,Model model
			,@PathVariable Long id
			,@PathVariable String name
			){
		
		request.setAttribute("searchtitle", id);
		
		request.setAttribute("titleset", "标签："+name);
		//设置页面描述
		request.setAttribute("siteTitle", name+"-坏银的博客");
		return "/front/article/tagsearch";
	}
	/**
	 *根据类别检索文章
	 */
	@RequestMapping(value="category/{id}/{name}", method = RequestMethod.GET)
	public String searchbycategory(HttpServletRequest request,Model model
			,@PathVariable Long id
			,@PathVariable String name
			){
		
		request.setAttribute("searchtitle", id);
		
		request.setAttribute("titleset", "类别："+name);
		//设置页面描述
		request.setAttribute("siteTitle", name+"-坏银的博客");
		return "/front/article/categorysearch";
	}
	/**
	 *获取验证码
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping(value="getValidateCode")
	public void getValidateCode(HttpServletRequest request,HttpServletResponse response,Model model) throws IOException{
		
		// 设置响应的类型格式为图片格式  
        response.setContentType("image/jpeg");  
        //禁止图像缓存。  
        response.setHeader("Pragma", "no-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0);  
		
		HttpSession session = request.getSession();  
        //生产验证码
        ValidateCode vCode = new ValidateCode(120,40,4,20);  
        session.setAttribute("code", vCode.getCode()); 
        vCode.write(response.getOutputStream());
        
	}
}
